package com.tbit.uqbike.client.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 国际化工具类
 *
 * @author LJ
 */
@Slf4j
public class CommonUtil {
    private static SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static String[] orderStateDes = new String[]{"待支付", "已支付", "支付中", "待确认", "已取消", "支付验证失败"};//订单状态描述
    private static String[] orderStateDesEN = new String[]{"wait to pay", "pay complated", "paying", "wait to sure", "cancel", "pay valid fail"};//订单状态描述

    public static String getStateDes(int state, Integer languageType) {
        try {
            if (languageType == null) {
                return orderStateDes[state];
            } else {
                return languageType == 1 ? orderStateDesEN[state] : orderStateDes[state];
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return "未知状态";
        }
    }


    //时间戳转换日期 
    public static Date stampToTime(String stamp) {
        try {
            if (StringUtils.isBlank(stamp)) {
                return new Date();
            }
            return s.parse(s.format(new Date(Long.parseLong(stamp))));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Date();
        }
    }

    /**
     * 获取当前请求的路径
     *
     * @param request
     * @param isServletPath 是否拼接相对路径（访问接口）
     * @param isQueryString 是否拼接参数
     * @return
     */
    public static String getRequestUrl(HttpServletRequest request, boolean isServletPath, boolean isQueryString) {
        String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
        if (isServletPath && StringUtils.isNotBlank(request.getServletPath())) {//是否拼接相对路径（访问接口）
            url += request.getServletPath();
        }
        if (isQueryString) {//是否拼接参数
            String paramsStr = (String) getReqParams(request, "String");
            if (paramsStr.length() > 0) {
                url += "?" + paramsStr;
            }
        }
        return url;
    }

    /**
     * 获取访问基本url（只有当前链接使用的协议、服务器地址 、端口号 、应用名称）
     *
     * @param request
     * @return
     */
    public static String getBaseUrl(HttpServletRequest request) {
        return getRequestUrl(request, false, false);
    }

    /**
     * 获取所有的参数值map
     *
     * @param request
     * @return
     */
    public static Map<String, String> getParamsMap(HttpServletRequest request) {
        return (Map<String, String>) getReqParams(request, "Map");
    }

    /**
     * 获取所有的参数值
     *
     * @param request
     * @return
     */
    private static Object getReqParams(HttpServletRequest request, String type) {
        Map<String, String[]> querys = request.getParameterMap();
        StringBuffer sb = new StringBuffer();
        Map<String, String> params = new HashMap<String, String>();
        if (querys != null && querys.size() > 0) {
            for (String key : querys.keySet()) {
                String[] values = querys.get(key);
                if (values != null && values.length > 0) {
                    if ("String".equals(type)) {
                        sb.append(sb.length() > 0 ? "&" : "").append(key + "=" + values[0]);
                    } else if ("Map".equals(type)) {
                        params.put(key, values[0]);
                    }
                }
            }
        }
        if ("String".equals(type)) {
            return sb.toString();
        } else if ("Map".equals(type)) {
            return params;
        } else {
            return null;
        }
    }

    /**
     * 根据指定长度截取字符串
     *
     * @param str
     * @param len
     * @return
     */
    public static String subStrByLen(String str, int len) {
        if (StringUtils.isBlank(str) || str.length() <= len) {
            return str;
        } else if (len - 3 <= 0) {
            return str.substring(0, len);
        } else {
            return str.substring(0, len - 3) + "...";
        }
    }

}
